//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "TermStructureConsistentModel.h"
using namespace Cephei::QL::Models;
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phTermStructureConsistentModel = NULL;
#endif
        _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure);
        _CtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _termStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()); 
        _ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (new QuantLib::TermStructureConsistentModel ( _termStructure ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (boost::shared_ptr<QuantLib::TermStructureConsistentModel>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTermStructureConsistentModel = NULL;
#endif
	_ppTermStructureConsistentModel = &childNative;
    
}
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (QuantLib::TermStructureConsistentModel& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTermStructureConsistentModel = NULL;
#endif
	_ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (&childNative);
    
    _TermStructureConsistentModelOwner = owner;
    
}

Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (CTermStructureConsistentModel^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTermStructureConsistentModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (copy->GetShared());
        
    }
}
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTermStructureConsistentModel = NULL;
#endif
	if (!t->IsSubclassOf(CTermStructureConsistentModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (QuantLib::Handle<QuantLib::TermStructureConsistentModel>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTermStructureConsistentModel = &childNative;
	_ppTermStructureConsistentModel = &static_cast<boost::shared_ptr<QuantLib::TermStructureConsistentModel>>(childNative.currentLink());
    
    _TermStructureConsistentModelOwner = owner;
}
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (QuantLib::Handle<QuantLib::TermStructureConsistentModel> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTermStructureConsistentModel = &childNative;
	_ppTermStructureConsistentModel = &static_cast<boost::shared_ptr<QuantLib::TermStructureConsistentModel>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::CTermStructureConsistentModel::CTermStructureConsistentModel (QuantLib::TermStructureConsistentModel childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTermStructureConsistentModel = NULL;
#endif
	_ppTermStructureConsistentModel = new boost::shared_ptr<QuantLib::TermStructureConsistentModel> (new QuantLib::TermStructureConsistentModel (childNative));
    
}
#endif

Cephei::QL::Models::CTermStructureConsistentModel::~CTermStructureConsistentModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTermStructureConsistentModel != NULL)
    {
	    delete _ppTermStructureConsistentModel;
        _ppTermStructureConsistentModel = NULL;
    }
}
Cephei::QL::Models::CTermStructureConsistentModel::!CTermStructureConsistentModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTermStructureConsistentModel != NULL)
    {
	    delete _ppTermStructureConsistentModel;
    }
}
QuantLib::TermStructureConsistentModel& Cephei::QL::Models::CTermStructureConsistentModel::GetReference ()
{
    if (_ppTermStructureConsistentModel == NULL) throw gcnew NativeNullException ();
	return **_ppTermStructureConsistentModel;
}
boost::shared_ptr<QuantLib::TermStructureConsistentModel>& Cephei::QL::Models::CTermStructureConsistentModel::GetShared ()
{
    if (_ppTermStructureConsistentModel == NULL) throw gcnew NativeNullException ();
	return *_ppTermStructureConsistentModel;
}
QuantLib::TermStructureConsistentModel* Cephei::QL::Models::CTermStructureConsistentModel::GetPointer ()
{
    if (_ppTermStructureConsistentModel == NULL) throw gcnew NativeNullException ();
	return &**_ppTermStructureConsistentModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TermStructureConsistentModel>& Cephei::QL::Models::CTermStructureConsistentModel::GetHandle ()
{
	if (_phTermStructureConsistentModel == NULL)
	{
		_phTermStructureConsistentModel = new Handle<QuantLib::TermStructureConsistentModel> (*_ppTermStructureConsistentModel);
	}
	return *_phTermStructureConsistentModel;
}
#endif
bool Cephei::QL::Models::CTermStructureConsistentModel::HasNative () 
{
	return (_ppTermStructureConsistentModel != NULL);
}

Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Models::CTermStructureConsistentModel::TermStructure::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure>& _rv = (Handle<QuantLib::YieldTermStructure>&)(*_ppTermStructureConsistentModel)->termStructure ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CYieldTermStructure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::ITermStructureConsistentModel^ Cephei::QL::Models::CTermStructureConsistentModel_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure)
{
    return gcnew CTermStructureConsistentModel ( termStructure);
}
